<template>
  <el-dialog
    title="修改手机"
    :visible.sync="visible"
    :append-to-body="true">
    <el-form :model="dataForm" :rules="tempRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="账号">
        <span>{{ userName }}</span>
      </el-form-item>
      <el-form-item label="新手机号" prop="mobile">
        <el-input  v-model="dataForm.mobile"></el-input>
      </el-form-item>
      <el-form-item label="短信验证码" prop="smsCode">
        <el-col :span="14">
        <el-input v-model="dataForm.smsCode"></el-input>
        </el-col>
        <el-col :span="10">
          <el-button @click="sendUpdatePhoneSMS()" :disabled="disabled" v-model="sendSMSBtnText">{{sendSMSBtnText}}</el-button>
        </el-col>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {clearLoginInfo} from '@/utils'

export default {
  data() {
    // let validateConfirmPassword = (rule, value, callback) => {
    //   if (this.dataForm.newPassword !== value) {
    //     callback(new Error('确认密码与新密码不一致'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      sendSMSBtnText: '发送短信',
        visible: false,
        countDown: {
          type: Number,
          default: 60
        },
        disabled: false,
        dataForm: {
          mobile: '',
          smsCode: ''
        },

        dataRule: {
          mobile: [
            {
              required: true,
              message: '手机号不能为空',
              trigger: 'blur'
            },
            {
              required: true,
              pattern: /^1[3456789]\d{9}$/,
              message: '手机号格式不正确',
              trigger: 'blur'
            }
          ],
          smsCode: [
            {
              required: true,
              message: '短信验证码不能为空',
              trigger: 'blur'
            }
          ]
        },
        tempRule: {},
        dataRule1: {
          mobile: [
            {
              required: true,
              message: '手机号不能为空',
              trigger: 'blur'
            },
            {
              required: true,
              pattern: /^1[3456789]\d{9}$/,
              message: '手机号格式不正确',
              trigger: 'blur'
            }
          ]
        },

      }
    },
    computed: {
      userName: {
        get () {
          return this.$store.state.user.name
        }
      },
      mainTabs: {
        get () {
          return this.$store.state.common.mainTabs
        },
        set (val) {
          this.$store.commit('common/updateMainTabs', val)
        }
      }
    },
    methods: {
      // 初始化
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      //发送短信验证码
      sendUpdatePhoneSMS(){
        this.tempRule = this.dataRule
        this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                async: false,
                url: '/sys/user/getSmsCode',
                method: 'post',
                data: {
                  'mobile': this.dataForm.mobile
                }
              }).then(({data}) => {
                if (data && data.code === 0) {
                  console.log("短信发送成功")
                } else {
                  this.$message({
                    message: data.msg,
                    type: 'success',
                    duration: 1500
                  })
                  this.getCaptcha()
                }
              })
            }
            })
        let timer = setInterval(()=>{
          this.time --;
          this.sendSMSBtnText = `${this.time}s后重新发送`
          if(this.time==0){
            this.sendSMSBtnText = `重新发送`
          }
          this.disabled = true
          if(this.time === 0){
            this.disabled = false;
            this.btnText = '重新发送';
            this.time = this.countDown.default
            clearInterval(timer)
          }
        }, 1000)

      },
      // 表单提交
      dataFormSubmit () {
        this.tempRule = this.dataRule1
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: '/sys/user/updateMobile',
              method: 'post',
              data: {
                'mobile': this.dataForm.mobile,
                'smsCode': this.dataForm.smsCode
              }
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$nextTick(() => {
                  this.mainTabs = []
                  clearLoginInfo()
                  this.$router.replace({ name: 'login' })
                })
              }
            })
          }
        })
      }
    },
    mounted(){
      this.time = this.countDown.default
    }
  }
</script>

